package com.delamer.facile.exe;

import com.delamer.facile.db.DBUtilBack;
import com.delamer.facile.db.DBUtilSwitch;
import com.delamer.facile.obj.ObjectBack;
import com.delamer.facile.obj.ObjectSwitch;
import com.delamer.utils.ItemUtil;
import com.delamer.utils.MsgUtil;
import com.delamer.utils.PermitUtil;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.TextComponent;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;

import java.io.File;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;

public class ExecutorBase {
    public void enderchest(CommandSender sender, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if (!PermitUtil.hasPermit(sender.getName(), "enderchest")) {
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        Player player = (Player) sender;
        player.openInventory(player.getEnderChest());
        sender.sendMessage(MsgUtil.msg("你打开了远程末影箱"));
    }

    public void ec(CommandSender sender, String[] args) {
        enderchest(sender, args);
    }

    public void workbench(CommandSender sender, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if (!PermitUtil.hasPermit(sender.getName(), "workbench")) {
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        Player player = (Player) sender;
        player.openWorkbench(null, true);
        sender.sendMessage(MsgUtil.msg("你打开了远程工作台"));
    }

    public void wb(CommandSender sender, String[] args) {
        workbench(sender, args);
    }

    public void fly(CommandSender sender, String[] args) {
        if (args.length == 1 && !(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("通过控制台使用该指令时，目标玩家不能为空"));
            return;
        }
        if (args.length > 1 && !sender.isOp()) {
            sender.sendMessage(MsgUtil.err("无OP权限的玩家无法使用该指令改变他人的飞行开关"));
            return;
        }
        if (!PermitUtil.hasPermit(sender.getName(), "fly")) {
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        Player target = args.length > 1 ? Bukkit.getPlayerExact(args[1]) : (Player) sender;
        if (target == null) {
            sender.sendMessage(MsgUtil.err("目标玩家不存在"));
            return;
        }
        target.setAllowFlight(!target.getAllowFlight());
        sender.sendMessage(MsgUtil.msg("指令使用成功，玩家 §f" + target.getName() + " §e当前的飞行开关状态为 " + (target.getAllowFlight() ? "§a开启" : "§c关闭")));
    }

    public void heal(CommandSender sender, String[] args) {
        if (args.length == 1 && !(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("通过控制台使用该指令时，目标玩家不能为空"));
            return;
        }
        if (args.length > 1 && !sender.isOp()) {
            sender.sendMessage(MsgUtil.err("无OP权限的玩家无法使用该指令改变他人的健康状态"));
            return;
        }
        if (!PermitUtil.hasPermit(sender.getName(), "heal")) {
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        Player target = args.length > 1 ? Bukkit.getPlayerExact(args[1]) : (Player) sender;
        if (target == null) {
            sender.sendMessage(MsgUtil.err("目标玩家不存在"));
            return;
        }
        target.setHealth(target.getMaxHealth());
        sender.sendMessage(MsgUtil.msg("指令使用成功，玩家 §f" + target.getName() + " §e的血量已被恢复到最大值"));
    }

    public void suicide(CommandSender sender, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if (!PermitUtil.hasPermit(sender.getName(), "suicide")) {
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        Player target = (Player) sender;
        target.setHealth(0);
    }

    public void back(CommandSender sender, String[] args) throws Exception {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if (!PermitUtil.hasPermit(sender.getName(), "back")) {
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        ObjectBack object = DBUtilBack.getBack(sender.getName());
        if (object == null) {
            sender.sendMessage(MsgUtil.err("传送到死亡坐标失败，不存在未使用的死亡回归坐标"));
            return;
        }
        Location location = new Location(Bukkit.getWorld(object.getWorldName()), Integer.valueOf(object.getLocationX()), Integer.valueOf(object.getLocationY()), Integer.valueOf(object.getLocationZ()));
        DBUtilBack.delBack(sender.getName());
        ((Player) sender).teleport(location);
        sender.sendMessage(MsgUtil.msg("传送到死亡坐标成功"));
    }

    public void enderchest_see(CommandSender sender, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if (!sender.isOp()) {
            sender.sendMessage(MsgUtil.err("无OP权限的玩家无法使用该指令"));
            return;
        }
        if (args.length == 0) {
            sender.sendMessage(MsgUtil.err("目标玩家不能为空"));
            return;
        }
        Player target = Bukkit.getPlayerExact(args[0]);
        if (target == null) {
            sender.sendMessage(MsgUtil.err("目标玩家不存在"));
            return;
        }
        Player player = (Player) sender;
        player.openInventory(target.getEnderChest());
        sender.sendMessage(MsgUtil.msg("你打开了玩家 §f" + target.getName() + " §e的末影箱"));
    }

    public void inventory_see(CommandSender sender, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if (!sender.isOp()) {
            sender.sendMessage(MsgUtil.err("无OP权限的玩家无法使用该指令"));
            return;
        }
        if (args.length == 0) {
            sender.sendMessage(MsgUtil.err("目标玩家不能为空"));
            return;
        }
        Player target = Bukkit.getPlayerExact(args[0]);
        if (target == null) {
            sender.sendMessage(MsgUtil.err("目标玩家不存在"));
            return;
        }
        Player player = (Player) sender;
        player.openInventory(target.getInventory());
        sender.sendMessage(MsgUtil.msg("你打开了玩家 §f" + target.getName() + " §e的物品栏"));
    }

    public void palette(CommandSender sender, String[] args) {
        if (!PermitUtil.hasPermit(sender.getName(), "palette")) {
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        TextComponent message = new TextComponent("§e调色板：§00§11§22§33§44§55§66§77§88§99§aa§bb§cc§dd§ee§ff§ll加粗§mm中划§nn下划§oo斜体§rr还原\n");//§ Alt0167
        TextComponent t0 = new TextComponent("§0■黑色");
        TextComponent t1 = new TextComponent("§1■海蓝");
        TextComponent t2 = new TextComponent("§2■深绿");
        TextComponent t3 = new TextComponent("§3■湖蓝");
        TextComponent t4 = new TextComponent("§4■血红");
        TextComponent t5 = new TextComponent("§5■品红");
        TextComponent t6 = new TextComponent("§6■橘黄");
        TextComponent t7 = new TextComponent("§7■浅灰");
        TextComponent t8 = new TextComponent("§8■深灰");
        TextComponent t9 = new TextComponent("§9■靛蓝");
        TextComponent ta = new TextComponent("§a■嫩绿");
        TextComponent tb = new TextComponent("§b■天蓝");
        TextComponent tc = new TextComponent("§c■苍红");
        TextComponent td = new TextComponent("§d■粉红");
        TextComponent te = new TextComponent("§e■金黄");
        TextComponent tf = new TextComponent("§f■白色");
        TextComponent tl = new TextComponent("§a§l■加粗§r");
        TextComponent tm = new TextComponent("§b§m■中划§r");
        TextComponent tn = new TextComponent("§c§n■下划§r");
        TextComponent to = new TextComponent("§d§o■斜体§r");
        TextComponent tr = new TextComponent("§r■清除格式");
        t0.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&0"));
        t1.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&1"));
        t2.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&2"));
        t3.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&3"));
        t4.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&4"));
        t5.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&5"));
        t6.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&6"));
        t7.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&7"));
        t8.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&8"));
        t9.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&9"));
        ta.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&a"));
        tb.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&b"));
        tc.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&c"));
        td.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&d"));
        te.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&e"));
        tf.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&f"));
        tl.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&l"));
        tm.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&m"));
        tn.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&n"));
        to.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&o"));
        tr.setClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, "&r"));
        message.addExtra(t0);
        message.addExtra("§r|");
        message.addExtra(t1);
        message.addExtra("§r|");
        message.addExtra(t2);
        message.addExtra("§r|");
        message.addExtra(t3);
        message.addExtra("§r|");
        message.addExtra(t4);
        message.addExtra("§r|");
        message.addExtra(t5);
        message.addExtra("§r|");
        message.addExtra(t6);
        message.addExtra("§r|");
        message.addExtra(t7);
        message.addExtra("\n");

        message.addExtra(t8);
        message.addExtra("§r|");
        message.addExtra(t9);
        message.addExtra("§r|");
        message.addExtra(ta);
        message.addExtra("§r|");
        message.addExtra(tb);
        message.addExtra("§r|");
        message.addExtra(tc);
        message.addExtra("§r|");
        message.addExtra(td);
        message.addExtra("§r|");
        message.addExtra(te);
        message.addExtra("§r|");
        message.addExtra(tf);
        message.addExtra("\n");

        message.addExtra(tl);
        message.addExtra("§r|");
        message.addExtra(tm);
        message.addExtra("§r|");
        message.addExtra(tn);
        message.addExtra("§r|");
        message.addExtra(to);
        message.addExtra("§r|");
        message.addExtra(tr);
        message.addExtra("\n");
        message.addExtra("§e点击对应内容可自动复制对应样式码，按下Ctrl+V组合键以粘贴样式码到聊天窗。");

        sender.spigot().sendMessage(message);
    }

    public void hat(CommandSender sender, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        Player player = (Player) sender;
        PlayerInventory inventory = player.getInventory();
        try {
            ItemStack headItem = inventory.getItem(39);
            ItemStack handItem = player.getInventory().getItemInMainHand();
            if (headItem != null || handItem == null) {
                sender.sendMessage(MsgUtil.err("该指令在头部装备栏为空且主手持有想要佩戴的道具时方可使用"));
                return;
            }
            inventory.setItem(39, handItem);
            inventory.setItem(inventory.getHeldItemSlot(), headItem);
            sender.sendMessage(MsgUtil.msg("操作成功，已将手中的道具佩戴到头上"));
        } catch (Exception e) {
            e.printStackTrace();
            sender.sendMessage(MsgUtil.err("该指令在头部装备栏为空且主手持有道具时方可使用"));
            return;
        }
    }

    public void quest(CommandSender sender, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        YamlConfiguration config = YamlConfiguration.loadConfiguration(new File(Bukkit.getPluginManager().getPlugin("DelamerFacile").getDataFolder(), "menu-quest.yml"));
        int size = config.getInt("menu.size");
        if (size < 1 || size > 6) {
            sender.sendMessage(MsgUtil.err("菜单打开失败 -> 尺寸配置错误"));
            return;
        } else {
            size *= 9;
        }
        Inventory inventory = Bukkit.createInventory(null, size, config.getString("menu.title"));
        ItemStack errorMenuItem = ItemUtil.createMenuItem(Bukkit.getPluginManager().getPlugin("DelamerFacile"), config.getString("menu.error.item").toUpperCase(), config.getString("menu.error.name"));
        if (errorMenuItem == null) {
            sender.sendMessage(MsgUtil.err("菜单打开失败 -> 填充栏配置错误:加载失败栏"));
            return;
        }
        ItemStack emptyMenuItem = ItemUtil.createMenuItem(Bukkit.getPluginManager().getPlugin("DelamerFacile"), config.getString("menu.empty.item").toUpperCase(), config.getString("menu.empty.name"));
        if (emptyMenuItem == null) {
            sender.sendMessage(MsgUtil.err("菜单打开失败 -> 填充栏配置错误：空栏"));
            return;
        }
        for (int i = 0; i < size; i++) {
            String prefix = "menu." + (i + 1);
            if (config.contains(prefix)) {
                ItemStack currentMenuItem = ItemUtil.createMenuItem(Bukkit.getPluginManager().getPlugin("DelamerFacile"), config.getString(prefix + ".item").toUpperCase(), config.getString(prefix + ".name"));
                if (currentMenuItem != null) {
                    if (config.contains(prefix + ".need")) {
                        Map<String, String> map = new HashMap<String, String>();
                        map.put("QuestMenuItem", "0");
                        String need = "";
                        for (String str : config.getStringList(prefix + ".need"))
                            need = need + (str + ",");
                        map.put("QuestNeed", need);
                        if (config.contains(prefix + ".quest_id"))
                            map.put("QuestId", config.getString(prefix + ".quest_id"));
                        if (config.contains(prefix + ".quest_time"))
                            map.put("QuestTime", config.getString(prefix + ".quest_time"));
                        currentMenuItem = ItemUtil.addQuest(Bukkit.getPluginManager().getPlugin("DelamerFacile"), currentMenuItem, map);
                    }
                    if (config.contains(prefix + ".event")) {
                        Map<String, String> map = new HashMap<String, String>();
                        if (config.contains(prefix + ".event.all"))
                            map.put("AllClickEvent", config.getString(prefix + ".event.all"));
                        if (config.contains(prefix + ".event.left"))
                            map.put("LeftClickEvent", config.getString(prefix + ".event.left"));
                        if (config.contains(prefix + ".event.shift_left"))
                            map.put("ShiftLeftClickEvent", config.getString(prefix + ".event.shift_left"));
                        if (config.contains(prefix + ".event.right"))
                            map.put("RightClickEvent", config.getString(prefix + ".event.right"));
                        if (config.contains(prefix + ".event.shift_right"))
                            map.put("ShiftRightClickEvent", config.getString(prefix + ".event.shift_right"));
                        if (config.contains(prefix + ".event.middle"))
                            map.put("MiddleClickEvent", config.getString(prefix + ".event.middle"));
                        if (config.contains(prefix + ".event.drop"))
                            map.put("DropClickEvent", config.getString(prefix + ".event.drop"));
                        if (config.contains(prefix + ".event.ctrl_drop"))
                            map.put("CtrlDropClickEvent", config.getString(prefix + ".event.ctrl_drop"));
                        currentMenuItem = ItemUtil.addEvent(Bukkit.getPluginManager().getPlugin("DelamerFacile"), currentMenuItem, map);
                    }
                    if (config.contains(prefix + ".lore")) {
                        currentMenuItem = ItemUtil.addLore(Bukkit.getPluginManager().getPlugin("DelamerFacile"), currentMenuItem, config.getStringList(prefix + ".lore"));
                    }
                    inventory.setItem(i, currentMenuItem);
                } else {
                    inventory.setItem(i, errorMenuItem);
                }
            } else {
                inventory.setItem(i, emptyMenuItem);
            }
        }
        ((Player) sender).openInventory(inventory);
    }

    public void nick(CommandSender sender, String[] args) {
        if (!(sender instanceof Player)) {
            sender.sendMessage(MsgUtil.err("该指令无法通过控制台使用"));
            return;
        }
        if (!PermitUtil.hasPermit(sender.getName(), "nick")) {
            sender.sendMessage(MsgUtil.err("未取得授权的玩家无法使用该指令"));
            return;
        }
        if (args.length > 0) {
            String nickName = args[0];
            try {
                ObjectSwitch object = DBUtilSwitch.getSwitch(new ObjectSwitch() {{
                    setUserName(((Player) sender).getName());
                    setSwitchName("color");
                }});
                if (object != null)
                    if (object.getSwitchState() > 0)
                        nickName = nickName.replaceAll("&", "§").replaceAll("§§", "&");
            } catch (Exception e) {
                e.printStackTrace();
            }
            ((Player) sender).setCustomName(nickName);
            sender.sendMessage(MsgUtil.msg("设置前置昵称成功 -> §r" + nickName));
        } else {
            ((Player) sender).setCustomName(null);
            sender.sendMessage(MsgUtil.msg("清空前置昵称成功"));
        }
    }
    public void broadcast(CommandSender sender, String[] args) {
        if (!sender.isOp()) {
            sender.sendMessage(MsgUtil.err("无OP权限的玩家无法使用该指令"));
            return;
        }
        if (args.length==0){
            sender.sendMessage(MsgUtil.err("请输入你要广播的内容"));
            return;
        }
        Bukkit.broadcastMessage(args[0].replaceAll("&","§").replaceAll("§§","&"));
    }

    public void bc(CommandSender sender, String[] args) {
        broadcast(sender, args);
    }

}
